import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";

let oneArr = [
  {
    id: "1",
    Children: [],
  },
  {
    id: "1",
    Children: [{ id: "01" }],
  },
  {
    id: "1",
    Children: [{ id: "001" }],
  },
  {
    id: "2",
    Children: [{ id: "02" }],
  },
  {
    id: "2",
    Children: [{ id: "002" }],
  },
  {
    id: "3",
    Children: [],
  },
  {
    id: "3",
    Children: [{ id: "03" }],
  },
  {
    id: "3",
    Children: [{ id: "003" }],
  },
  {
    id: "3",
    Children: [{ id: "0003" }],
  },
  {
    id: "4",
    Children: [{ id: "04" }],
  },
];

let obj = {};
let twoArr = [];

oneArr.forEach(function (item, suffix) {
  //根据对象的属性是唯一的，将值作为对象的属性名
  if (!obj[item.dictCode]) {
    let arr = [];
    arr.push(item);
    twoArr.push(arr);
    obj[item.dictCode] = item;
  } else {
    twoArr.forEach(function (value, index) {
      //如果已经存在  就循环新组的值将值插入属性相同的数组里   为了防止重复添加   只要和第一个比较就可以了
      if (value[0].dictCode === item.dictCode) {
        value.push(item);
      }
    });
  }
});

let threeArr = [];

twoArr.forEach((item) => {
  if (item.length === 1) {
    threeArr.push(item[0]);
  } else {
    item.forEach((childrenItem) => {
      if (childrenItem.Children.length > 0) {
        threeArr.push(childrenItem);
      }
    });
  }
});

let newObj = {};
let newReturnArr = [];
threeArr.forEach(function (item, suffix) {
  //根据对象的属性是唯一的，将值作为对象的属性名
  if (!newObj[item.id]) {
    var arr = [];
    arr.push(item);
    newReturnArr.push(arr);
    newObj[item.id] = item;
  } else {
    newReturnArr.forEach(function (value, index) {
      //如果已经存在  就循环新组的值将值插入属性相同的数组里   为了防止重复添加   只要和第一个比较就可以了
      if (value[0].id === item.id) {
        value.push(item);
      }
    });
  }
});

console.log(newReturnArr, "===");

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
